/*
 * GrantPanel.java
 *
 * Created on April 25, 2007, 11:20 AM
 */

package org.glossitope.container.ui;

import org.glossitope.container.PermissionManager;

/**
 *
 * @author  joshy
 */
public class GrantPanel extends javax.swing.JPanel {
    private PermissionsPanel panel;
    public GrantPanel(String message, PermissionsPanel panel) {
        this();
        this.panel = panel;
        this.message.setText(message);
    }
    /** Creates new form GrantPanel */
    public GrantPanel() {
        initComponents();
    }
    
    private boolean answered;
    public boolean isAnswered() {
        return answered;
    }
    
    private PermissionManager.Permission selectedPermission = PermissionManager.Permission.NO;
    public PermissionManager.Permission getSelectedPermission() {
        return selectedPermission;
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {

        message = new javax.swing.JLabel();
        deny = new javax.swing.JButton();
        always = new javax.swing.JCheckBox();
        allow = new javax.swing.JButton();

        setBorder(javax.swing.BorderFactory.createMatteBorder(0, 1, 1, 1, new java.awt.Color(0, 0, 0)));

        message.setText("Message");

        deny.setText("Deny");
        deny.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                denyActionPerformed(evt);
            }
        });

        always.setText("always");
        always.setMargin(new java.awt.Insets(0, 0, 0, 0));

        allow.setText("Allow");
        allow.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                allowActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(always)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 168, Short.MAX_VALUE)
                .add(allow)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(deny))
            .add(message, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 388, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .add(message, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(always)
                    .add(deny)
                    .add(allow)))
        );
    }// </editor-fold>//GEN-END:initComponents
    
private void allowActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_allowActionPerformed
    synchronized(this) {
        if(always.isSelected()) {
            selectedPermission = PermissionManager.Permission.ALWAYS;
        } else {
            selectedPermission = PermissionManager.Permission.YES;
        }
        panel.removeGrant(this);
        this.answered = true;
        this.notifyAll();// TODO add your handling code here:
    }
}//GEN-LAST:event_allowActionPerformed

    private void denyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_denyActionPerformed
        synchronized(this) {
            if(always.isSelected()) {
                selectedPermission = PermissionManager.Permission.NEVER;
            } else {
                selectedPermission = PermissionManager.Permission.NO;
            }
            panel.removeGrant(this);
            this.answered = true;
            this.notifyAll();// TODO add your handling code here:
        }
}//GEN-LAST:event_denyActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton allow;
    private javax.swing.JCheckBox always;
    private javax.swing.JButton deny;
    private javax.swing.JLabel message;
    // End of variables declaration//GEN-END:variables
    
}
